<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>点击地图获取地址和经纬度map，address，lng，lat</title>
    <meta name="robots" content="noindex, nofollow">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <!-- 将百度地图API引入，设置好自己的key -->
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=5phgXiGFezYX80IUIjnvLss6oKNRlFNS"></script>
</head>
<body>
 
<div class="main-div">
    <form method="post" action="" name="theForm" enctype="multipart/form-data" onsubmit="return validate()">
        <table cellspacing="1" cellpadding="3" width="100%">
            <tr>
                <td class="label">经度</td>
                <td><input type="text" name="lng" id="lng" value=""/>
                </td>
            </tr>
            <tr>
                <td class="label">纬度</td>
                <td><input type="text" name="lat" id="lat" value=""/>
                </td>
            </tr>
            <tr>
                <td class="label">地址</td>
                <td>
                    <input type='text' value='' name='sever_add' id='sever_add' readonly>
                    <input type='button' value='点击显示地图获取地址经纬度' id='open'>
                </td>
            </tr>
        </table>
    </form>
    <div id='allmap' style='width: 50%; height: 50%; position: absolute;'></div>
</div>
<script type="text/javascript">
    var map = new BMap.Map("allmap");
    var geoc = new BMap.Geocoder();   //地址解析对象
    var markersArray = [];
    var geolocation = new BMap.Geolocation();
 
    var point = new BMap.Point(116.331398, 39.897445);
    map.centerAndZoom(point, 12); // 中心点
    geolocation.getCurrentPosition(function (r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            var mk = new BMap.Marker(r.point);
            map.addOverlay(mk);
            map.panTo(r.point);
            map.enableScrollWheelZoom(true);
        }
        else {
            console.log('failed' + this.getStatus());
        }
    }, {enableHighAccuracy: true})


    map.addEventListener("click", showInfo);
    //清除标识
    function clearOverlays() {
        if (markersArray) {
            for (i in markersArray) {
                map.removeOverlay(markersArray[i])
            }
        }
    }
    //地图上标注
    function addMarker(point) {
        var marker = new BMap.Marker(point);
        markersArray.push(marker);
        clearOverlays();
        map.addOverlay(marker);
    }
    //点击地图时间处理
    function showInfo(e) {
        document.getElementById('lng').value = e.point.lng;//Lng
        document.getElementById('lat').value =  e.point.lat;//lat
        geoc.getLocation(e.point, function (rs) {
            var addComp = rs.addressComponents;
            var address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
            console.log(address)
            document.getElementById('sever_add').value = address;
       
        });
        addMarker(e.point);
    }
</script>

</body>
</html>
